package com.hnkywl.sts.action.htdygl;

import com.hnkywl.sts.entity.htgl.Htys;
import com.hnkywl.sts.service.htdygl.HtysdyService;
import com.hnkywl.sts.util.Constant;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import com.hnkywl.sts.entity.base.Sjzdsy;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-4-15
 * Time:  上午 11:28
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/htdygl/htysdy")
public class HtysdyAction extends SimpleActionSupport {

    // 合同元素定义service
    private HtysdyService htysdyService;

    // 合同元素实体
    private Htys htys;

    // 合同定义id
    private Long htdyId;

    // 修改合同元素id
    private Long htysdyId;

    // 合同元素名称
    private String htysmc;

    // 输入框取值
    private String inputQz;

    // 选择框取值
    private String selectQz;

    // 默认值必输
    private String ismrz;

    //默认值可以不输
    private String nomrz;

    // 数据字典list
    private List<Sjzdsy> sjzdsyList;

    // 分页数
    private int page;

    // 分页组件 页面大小
    private int rows;


    // 合同元素定义的查询分页显示
    @Action("htysdyList")
    public void htysdyList() {
        try {
            this.renderJson(this.htysdyService.findAllHtysdy(htdyId, htysmc, page, rows));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 查询合同元素值
    @Action("htysdy_content")
    public String htysdy_content() {
        try {
            if (StringUtils.isNotEmpty(htysdyId.toString())) {
                // 查询数据字典
                sjzdsyList = this.htysdyService.findAllSjzdsy();
                // 查询数据元素
                htys = this.htysdyService.findHtysdyById(htysdyId);
                return SUCCESS;
            } else {
                renderJson("{\"statusCode\":\"300\", \"message\":\"" + "合同元素id为空！" + "\"}");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
        return null;
    }

    // 修改合同元素
    @Action("modifyHtysdy")
    public void modifyHtysdy() {
        if (null != htysdyId && htysdyId > 0) {
            try {
                // 判断是否是数据字典
                if (null != htys.getYxlx()) {
                    if (htys.getYxlx() == Constant.XXLX_DATA) {
                        htys.setQz(selectQz);
                    } else if (htys.getYxlx() == Constant.XXLX_DATASET || htys.getYxlx() == Constant.XXLX_DROPDOWN) {
                        htys.setQz(inputQz);
                    } else {
                        htys.setQz("");
                    }
                }

                //  判断默认值是否必输
                if ((null != htys.getIsread() && htys.getIsread().equals("0"))) {
                    htys.setMrz(ismrz);
                } else {
                    htys.setMrz(nomrz);
                }
                this.htysdyService.saveHtysdy(htys);
                renderText("{\"statusCode\":\"200\", \"message\":\"" + "修改合同元素成功！" + "\"}");
            } catch (Exception e) {
                e.printStackTrace();
                renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
            }

        } else {
            renderText("{\"statusCode\":\"300\", \"message\":\"" + "合同元素id为空，修改错误！" + "\"}");
        }
    }

    @Autowired
    public void setHtysdyService(HtysdyService htysdyService) {
        this.htysdyService = htysdyService;
    }

    public Htys getHtys() {
        return htys;
    }

    public void setHtys(Htys htys) {
        this.htys = htys;
    }

    public Long getHtdyId() {
        return htdyId;
    }

    public void setHtdyId(Long htdyId) {
        this.htdyId = htdyId;
    }

    public String getHtysmc() {
        return htysmc;
    }

    public void setHtysmc(String htysmc) {
        this.htysmc = htysmc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public Long getHtysdyId() {
        return htysdyId;
    }

    public void setHtysdyId(Long htysdyId) {
        this.htysdyId = htysdyId;
    }

    public List<Sjzdsy> getSjzdsyList() {
        return sjzdsyList;
    }

    public void setSjzdsyList(List<Sjzdsy> sjzdsyList) {
        this.sjzdsyList = sjzdsyList;
    }

    public String getInputQz() {
        return inputQz;
    }

    public void setInputQz(String inputQz) {
        this.inputQz = inputQz;
    }

    public String getSelectQz() {
        return selectQz;
    }

    public void setSelectQz(String selectQz) {
        this.selectQz = selectQz;
    }

    public String getIsmrz() {
        return ismrz;
    }

    public void setIsmrz(String ismrz) {
        this.ismrz = ismrz;
    }

    public String getNomrz() {
        return nomrz;
    }

    public void setNomrz(String nomrz) {
        this.nomrz = nomrz;
    }
}
